<template>
    <el-form :model="shop" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="店名" prop="shopname">
                    <el-input v-model="shop.shopname" placeholder="请输入店名" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="密码" prop="pwd">
                    <el-input v-model="shop.pwd" placeholder="请输入密码" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="审核状态" prop="checked">
                    <el-radio-group v-model="shop.checked">
                        <el-radio value="0">审核中</el-radio>
                        <el-radio value="1">已通过</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="地址" prop="press">
                    <el-input v-model="shop.address" placeholder="请输入地址" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="shop.phone" placeholder="请输入电话" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="营业状态" prop="state">
                    <el-input v-model="shop.state" placeholder="请输入状态" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="头像" prop="picture">
            <el-upload drag style="width:100%"
                       action="/api/file/upload"
                       :on-success="handleFileUploadSuccess"
            >
                <el-icon class="el-icon--upload"><upload-filled/></el-icon>
                <div class="el-upload__text">将文档拖拽到此处，或 <em>点击上传</em></div>
                <template #tip>
                    <div class="el-upload__tip">支持 jpg/png 格式， 大小不超过 5M</div>
                </template>
            </el-upload>
        </el-form-item>
    </el-form>

    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import {inject, ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "closeDialog",	// 关闭 dialog
    "flushPage"		// 刷新页面
]);

const shop = ref({
    shopname: "",
    pwd: "",
    phone: "",
    checked: "",
    address: "",
    score: "",
    state: "",
    picture: ""
})

const handleUpdateData = (obj) => {
    shop.value = obj;
}
const handleFileUploadSuccess = (fileName) => {
    if (fileName != null || fileName != '') {
        shop.value.picture= fileName;
        ElMessage({type: 'success',message:'文件上传成功'})
    }else{
        ElMessage({type: 'error',message:'文件上传失败'})
    }
}
// 暴露给父组件使用的方法
defineExpose({
    handleUpdateData,
})

// 取消
const doCancel = () => {
    emits("closeDialog");
}

// 更新
const doSave = async () => {
    const {data} = await axios.put('/api/shop', shop.value);
    if (data > 0) {
        emits("flushPage");				// 触发事件： 刷新页面
        emits("closeDialog");			// 触发事件：关闭 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>

<style scoped>
</style>